﻿using System;
using System.Linq;
using System.Windows.Forms;
using System.Collections.Generic;

namespace MainModule
{
   static class Program
   {
      internal enum CommandLineArgs
      {
         Test,
         Info,
         Update,
         Silent,
         Profile,
         AutoRun,
         Language,
         LocalNetwork,
      }

      internal static List<string> CmdArgs = Environment.GetCommandLineArgs().ToList();

      internal static bool AutoRun
      {
         get;
         private set;
      }

      internal static bool CheckInstallationMode
      {
         get;
         private set;
      }

      internal static bool CheckCommandLineArg(CommandLineArgs arg)
      {
         return GetCommandLineArg(arg) != null;
      }

      internal static string GetCommandLineArg(CommandLineArgs arg)
      {
         int i = 0;
         string value = null;
         
         switch (arg)
         {
            case CommandLineArgs.Test:
            {
               i = CmdArgs.IndexOf("/test");
               if (i < 0) i = CmdArgs.IndexOf("-test");
               break;
            }
            case CommandLineArgs.Silent:
            {
               i = CmdArgs.IndexOf("/silent");
               if (i < 0) i = CmdArgs.IndexOf("/s");
               if (i < 0) i = CmdArgs.IndexOf("-s");
               if (i < 0) i = CmdArgs.IndexOf("/q");
               if (i < 0) i = CmdArgs.IndexOf("-q");
               if (i < 0) i = CmdArgs.IndexOf("-silent");
               break;
            }
            case CommandLineArgs.Info:
            {
               i = CmdArgs.IndexOf("/info");
               if (i < 0) i = CmdArgs.IndexOf("-info");
               break;
            }
            case CommandLineArgs.Update:
            {
               i = CmdArgs.IndexOf("/update");
               if (i < 0) i = CmdArgs.IndexOf("/u");
               if (i < 0) i = CmdArgs.IndexOf("-u");
               if (i < 0) i = CmdArgs.IndexOf("-update");
               break;
            }
            case CommandLineArgs.Profile:
            {
               i = CmdArgs.IndexOf("/profile");
               if (i < 0) i = CmdArgs.IndexOf("/p");
               if (i < 0) i = CmdArgs.IndexOf("-p");
               if (i < 0) i = CmdArgs.IndexOf("-profile");
               break;
            }
            case CommandLineArgs.AutoRun:
            {
               i = CmdArgs.IndexOf("/autorun");
               if (i < 0) i = CmdArgs.IndexOf("/a");
               if (i < 0) i = CmdArgs.IndexOf("-a");
               break;
            }
            case CommandLineArgs.Language:
            {
               i = CmdArgs.IndexOf("/lang");
               if (i < 0) i = CmdArgs.IndexOf("-lang");
               break;
            }
            case CommandLineArgs.LocalNetwork:
            {
               i = CmdArgs.IndexOf("/localnetwork");
               if (i < 0) i = CmdArgs.IndexOf("/ln");
               if (i < 0) i = CmdArgs.IndexOf("-ln");
               if (i < 0) i = CmdArgs.IndexOf("-localnetwork");
               break;
            }
         }
         if (i >= 0)
         {
            value = "";
            if (i + 1 < CmdArgs.Count && !CmdArgs[i + 1].StartsWith("/") && !CmdArgs[i + 1].StartsWith("-"))
            {
               value = CmdArgs[i + 1];
            }
         }
         return value;
      }

      [STAThread]
      static int Main()
      {
         //System.Threading.Thread.Sleep(8000); // uncomment to attach debugger during this sleep

         CmdArgs.ForEach(s => s = s.ToLower());

         try
         {
            if (CheckCommandLineArg(CommandLineArgs.Update))
            {
               CheckInstallationMode = true;

               try
               {
                  return (int)Deployment.DownloadUpdates();
               }
               catch
               {
                  return (int)InstallationState.Error;
               }
            }
            else if (CheckCommandLineArg(CommandLineArgs.Info))
            {
               CheckInstallationMode = true;

               Deployment.SubmitInstallInfo();

               return 0;
            }
            else
            {
               if (Deployment.InstallUpdatesIfAvailable()) return 0;

               AutoRun = Program.CheckCommandLineArg(Program.CommandLineArgs.AutoRun);

               Application.EnableVisualStyles();
               Application.SetCompatibleTextRenderingDefault(false);

               new InternetCommunicator();

               Application.Run();

               return Environment.ExitCode;
            }
         }
         catch (Exception err)
         {
            ErrorViewer.ShowFatal("Application failed to continue:", err);

            return -1;
         }
      }
   }
}
